<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-8-11
 * Time: 下午8:26
 */

namespace PddWechat\Models\Mall\Service;


use Dai\Framework\Base\BaseSessionInfo;
use Dai\Framework\Library\RedisExt;
use PddWechat\Library\PddLibrary;

class Goods
{
    /**
     * @param $basePageInfo
     */
    public function execute($basePageInfo) {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        $openId = $sessionData->openId;

        $key = "generate_goods_openId_$openId";
        $params = $basePageInfo->params;

        $cpsSign = RedisExt::getInstance()->get($key);
        if( $cpsSign == null) {
            $url =  PddLibrary::addGoodsUrl($params->goodsId, $sessionData->inviteCode );
            $urlArray = explode("&", $url);
            foreach ($urlArray as $urlItem) {
                $itemArr = explode("=", $urlItem);
                if( $itemArr[0] == "cpsSign") {
                    $cpsSign = $itemArr[1];
                    RedisExt::getInstance()->setex($key, 86400 * 10, $cpsSign);
                }
            }
        }

        $url = sprintf("https://mobile.yangkeduo.com/duo_coupon_landing.html?goods_id=%d&pid=1884871_22329699&customParameters=%s&cpsSign=%s&duoduo_type=2&launch_wx=1", $params->goodsId,$sessionData->inviteCode, $cpsSign);
        header("Location: $url");
        exit;
    }
}